#include <iostream>
#include <fstream>
#include <ctime>

#include "GameModule.h"

using namespace std;

void GameModule::printAllModules() {
  int i;

  cout << (1+countSubModules()) << " module(s) loaded." << endl << endl;

  cout << "* " << getName() << " Module" << endl;
  cout << getCredits() << endl << endl;

  for (i=0; i < countSubModules(); i++) {
    cout << "* " << modules[i]->getName() << " Module" << endl;
    cout << modules[i]->getCredits() << endl << endl;
  }
}

void GameModule::init() {
  modules = new DataModule*[countSubModules()];
  loadSubModules();
}

GameModule::~GameModule() { }

void GameModule::destroy() {
  int i;
  for (i=0; i<countSubModules(); i++)
    delete modules[i];
  delete[] modules;
}

void GameModule::readFromRom(Rom &rom) {
  int i;
  clock_t start;
  double diff;
  cout << "Reading from ROM" << endl;
  for (i=0; i<countSubModules(); i++) {
    cout << "* " << modules[i]->getName();
    flush(cout);
    start = clock();
    modules[i]->readFromRom(rom);
    diff = (clock() - start) / ((double) CLOCKS_PER_SEC);
    cout << " (" << diff << " s)" << endl;
  }
}

void GameModule::writeToRom(Rom &rom) {
  int i;
  clock_t start;
  double diff;
  cout << "Writing to ROM" << endl;
  for (i=0; i<countSubModules(); i++) {
    cout << "* " << modules[i]->getName();
    flush(cout);
    start = clock();
    modules[i]->writeToRom(rom);
    diff = (clock() - start) / ((double) CLOCKS_PER_SEC);
    cout << " (" << diff << " s)" << endl;
  }
}

void GameModule::readFromDat(char* dirName, map<string, string> &fns, map<string, int> &labels) {
  int i;
  clock_t start;
  double diff;
  cout << "Reading from files" << endl;
  for (i=0; i<countSubModules(); i++) {
    cout << "* " << modules[i]->getName();
    flush(cout);
    start = clock();
    modules[i]->readFromDat(dirName, fns, labels);
    diff = (clock() - start) / ((double) CLOCKS_PER_SEC);
    cout << " (" << diff << " s)" << endl;
  }
}

void GameModule::writeToDat(char* dirName, map<string, string> &fns) {
  int i;
  clock_t start;
  double diff;
  cout << "Writing to files" << endl;
  for (i=0; i<countSubModules(); i++) {
    cout << "* " << modules[i]->getName();
    flush(cout);
    start = clock();
    modules[i]->writeToDat(dirName, fns);
    diff = (clock() - start) / ((double) CLOCKS_PER_SEC);
    cout << " (" << diff << " s)" << endl;
  }
}
